﻿#region License Information

/* •——————————————————————————————————————————————————————————————————————————————————————————————————————————————————————•
   | Copyright Aptillon, Inc.                                                                                        |
   | This file is part of the CKS:API project - cksapi.codeplex.com                                                       |
   | Usage, modification and distribution subject to the license terms available here: http://cksapi.codeplex.com/license |
   |                                                                                                                      |
   | Software distributed under the License is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND,             |
   | either express or implied.                                                                                           |
   |                                                                                                                      |
   | THIS MESSAGE MAY NOT BE LEGALLY REMOVED                                                                              |
   •——————————————————————————————————————————————————————————————————————————————————————————————————————————————————————• */

#endregion


using System;
using CKS.API;
using System.Web;
using System.Web.UI;
using Microsoft.SharePoint;
using System.IO;
using CKS.API.Logging;
using CKS.API.Exceptions;
using CKS.API.Contracts;
using Microsoft.SharePoint.WebControls;

namespace CKS.API.Utilities
{

    public class CKSApiUtilities
    {

        private static bool? _isInSandbox;
        public static bool IsInSandbox
        {
            get
            {
                object lockObj = new object();
                if (!_isInSandbox.HasValue)
                {
                    lock (lockObj)
                    {
                        if (!_isInSandbox.HasValue)
                        {
                            _isInSandbox = System.AppDomain.CurrentDomain.FriendlyName.Contains("Sandbox") ? true : false;
                        }
                    }
                }

                return _isInSandbox.Value;
            }
        }


        public static void EnsureSPContext()
        {


            if (Check.ThatIsNotNull(SPContext.Current)) return;

            if (Check.ThatIsNotNull(HttpContext.Current))
            {
                using (SPSite site = new SPSite(HttpContext.Current.Request.Url.ToString()))
                {
                    using (SPWeb web = site.OpenWeb())
                    {
                        SPControl.SetContextWeb(HttpContext.Current, web);
                        return;
                    }
                }
            }
            else
            {
                throw new InvalidOperationException("No valid HttpContext.");
            }

            throw new InvalidOperationException("Unable to retrieve a valid SPContext.");
        }

        public static void EnsureSPContext(string webUrl)
        {
            if (Check.ThatIsNotNull(SPContext.Current)) return;

            HttpRequest httpRequest = new HttpRequest("", webUrl, "");
            HttpContext.Current = new HttpContext(httpRequest, new HttpResponse(new StringWriter()));
            // EnsureSPContext();
            if (Check.ThatIsNotNull(HttpContext.Current))
            {
                using (SPSite site = new SPSite(HttpContext.Current.Request.Url.ToString()))
                {
                    using (SPWeb web = site.OpenWeb())
                    {
                        SPControl.SetContextWeb(HttpContext.Current, web);
                        return;
                    }
                }
            }

            throw new InvalidOperationException("Unable to retrieve a valid SPContext for specified Url");

        }

    }
}
